package com.example.onenetdht11;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.onenetdht11.dao.UserDao2;
import com.example.onenetdht11.entity.User2;

public class RegisterActivity extends AppCompatActivity {

    Button btnRegisterRegister;
    ImageButton btnBackRegister;
    EditText etAccountRegister,etPasswordRegister;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initListener();
    }

    private void initView() {
        etAccountRegister = findViewById(R.id.et_account_register);
        etPasswordRegister = findViewById(R.id.et_password_register);
        btnBackRegister = findViewById(R.id.btn_back_register);
        btnRegisterRegister = findViewById(R.id.btn_register_register);
        btnRegisterRegister.setEnabled(false);
    }

    private void initListener() {
        @SuppressLint("NonConstantResourceId") View.OnClickListener listener = v -> {
            switch (v.getId()){
                case R.id.btn_back_register:
                    backRegister();
                    break;
                case R.id.btn_register_register:
                    register();
                    break;
            }
        };
        btnBackRegister.setOnClickListener(listener);
        btnRegisterRegister.setOnClickListener(listener);

        TextWatcher textlisterner = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etPasswordRegister.length() >0 && etAccountRegister.length() == 11){
                    btnRegisterRegister.setEnabled(true);
                }else {
                    btnRegisterRegister.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        etPasswordRegister.addTextChangedListener(textlisterner);
        etAccountRegister.addTextChangedListener(textlisterner);
    }

    /**
     * 注册账号并更新到数据库中
     */
    private void register() {
        String username = etAccountRegister.getText().toString();
        String password = etPasswordRegister.getText().toString();
        User2 user = new User2(username,password);
        UserDao2 userDao2 = new UserDao2(this);
        long count = userDao2.register(user);
        if(count != 0){
            Toast.makeText(getApplicationContext(), "恭喜您,注册成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext().getApplicationContext(), "该账号已注册", Toast.LENGTH_SHORT).show();
        }
//        System.out.println("影响行数为：" + count);
        backRegister();
    }

    /**
     * 从注册界面跳转到登陆界面
     */
    private void backRegister() {
//        Intent intent_back = new Intent();
//        intent_back.setClass(this,LoginActivity.class);
//        startActivity(intent_back);
        finish();
    }
}
